package com.rui.member.controller;


import com.rui.common.vo.Result;

import com.rui.member.entity.Member;
import com.rui.member.service.MemberService;
import com.rui.member.vo.MemberQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@Controller
@RequestMapping("/member")
public class Membercontroller {

    @Autowired
    private MemberService memberService;


    @GetMapping("")
    public  String toMemberListUI(){
        return "member/memberList";
    }

    @GetMapping("/list")
    @ResponseBody
     public Result<Object> getMemberList(MemberQuery param){

        List<Member> list = memberService.getMemberList(param);

        Long count = memberService.countMemberList(param);
        return Result.success(list,count);
     }
     @PostMapping("")//新增数据时，用post请求
     @ResponseBody
    public Result<Object> addMember(Member member){
        memberService.addMember(member);
        return Result.success("新增会员成功！");
    }
    @GetMapping("add/ui")
    public String toAddUI(Model model){
        return "member/addMember";
    }
    //删除会员
    @DeleteMapping("/{delMember}")
    @ResponseBody
    public Result<Object> deleteMember(@PathVariable("delMember") String memberAccount) {
        memberService.deleteByMemberAccount(memberAccount);
        return Result.success("删除会员成功");
    }

    @GetMapping("/{account}")
    public String getMemberByaccount(@PathVariable("account") Integer account,Model model){
       Member member =  memberService.getMemberByaccount(account);
       model.addAttribute("member",member);
       return "member/memberEdit";
    }

    @PutMapping("")
    @ResponseBody
    public Result<Object> updateMember(Member member){
        memberService.updateMember(member);
        return  Result.success("会员信息修改成功");
    }
}
